package com.controller;

import com.pojo.ClassRoom;
import com.pojo.Course;
import com.pojo.R;
import com.service.ClassRoomService;
import com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/class")
public class ClassRoomController {
    @Autowired
    private ClassRoomService classRoomService;
    @Autowired
    private CourseService courseService;
    @GetMapping("/allClass")
    public R list() {
        List<ClassRoom> list = classRoomService.queryAllClassRoom();
        return R.ok(list);
    }

    @PostMapping("/addClass")
    public R addClass(@RequestBody ClassRoom classRoom) {
        classRoomService.addClassRoom(classRoom);
        return R.ok();
    }

    @PostMapping("/del/{id}")
    public R deleteClass(@PathVariable("id") long id) {
        classRoomService.deleteClassRoom(id);
        return R.ok();
    }

    @PostMapping("/updateClass")
    public R update(@RequestBody ClassRoom classRoom) {
        classRoomService.updateClassRoom(classRoom);
        return R.ok();
    }

    @GetMapping("/getClassById")
    public R getClassById(@RequestParam("id") long id) {
        ClassRoom classRoom = classRoomService.queryById(id);
        return R.ok(classRoom);
    }
    @GetMapping("/getClassInfo")
    public R getClassInfo(@RequestParam("id") long id) {
        Map<String, Object> resultMap = new HashMap<>();
        ClassRoom classRoom = classRoomService.queryById(id);
        List<Course> courseList = courseService.getListById(id);
        resultMap.put("info",classRoom);
        resultMap.put("list",courseList);
        return R.ok(resultMap);
    }
}
